package com.wx_shop.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx_shop.api.comm.Constants;
import com.wx_shop.api.comm.Constants.COLUMN_PUB;
import com.wx_shop.api.comm.ServiceCode;
import com.wx_shop.api.domain.CmsColumn;
import com.wx_shop.api.domain.CmsColumnPub;
import com.wx_shop.api.domain.ProProduct;
import com.wx_shop.api.domain.gen.CmsColumnCriteria;
import com.wx_shop.api.domain.gen.CmsColumnPubCriteria;
import com.wx_shop.api.mapper.CmsColumnMapper;
import com.wx_shop.api.mapper.CmsColumnPubMapper;
import com.wx_shop.api.mapper.ProProductMapper;
import com.wx_shop.api.service.CmsPubService;
import com.wx_shop.api.vo.BannerVo;
import com.wx_shop.api.vo.BaseRes;
import com.wx_shop.api.vo.ProductVo;

@Service
public class CmsPubServiceImpl implements CmsPubService {

	@Autowired
	private CmsColumnMapper cmsColumnMapper;
	
	@Autowired
	private CmsColumnPubMapper cmsColumnPubMapper;
	
	@Autowired
	private ProProductMapper productMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> BaseRes<T> fetchPubByType(String queryType) {
		
		BaseRes<T> res = new BaseRes<T>();
		
		CmsColumnCriteria pCmsColumnCriteria = new CmsColumnCriteria();
		pCmsColumnCriteria.createCriteria().andIdentityEqualTo(queryType);
		
		List<CmsColumn> columns = cmsColumnMapper.selectByExample(pCmsColumnCriteria);
		if(columns.size() > 0) {
			CmsColumn mCmsColumn = columns.get(0);
			
			CmsColumnPubCriteria pCmsColumnPubCriteria = new CmsColumnPubCriteria();
			pCmsColumnPubCriteria.setOrderByClause(" sort desc ");
			pCmsColumnPubCriteria.createCriteria().andColumnIdEqualTo(mCmsColumn.getId());
			List<CmsColumnPub> pubs = cmsColumnPubMapper.selectByExample(pCmsColumnPubCriteria);
			
			List<Object> pubOjbs = new ArrayList<Object>();
			
			
			// === 对象内容设置 ===
			TypeSwitch mTypeSwitch = new TypeSwitch(){

				@Override
				public ProductVo reProductVo(CmsColumnPub pub, String type) {
					ProProduct mProProduct = productMapper.selectByPrimaryKey(pub.getEntityId());
					
					ProductVo product = new ProductVo();
					product.setId(pub.getEntityId());
					product.setTitle(pub.getTitle());
					product.setPicture(mProProduct.getPicture());
					product.setPrice(mProProduct.getPrice());
					
					return product;
				}

				@Override
				public BannerVo reBannerVo(CmsColumnPub pub, String type) {
					BannerVo mBannerVo = new BannerVo();
					mBannerVo.setId(pub.getEntityId());
					mBannerVo.setType(pub.getType());
					mBannerVo.setTitle(pub.getTitle());
					mBannerVo.setImage(pub.getImage());
					
					return mBannerVo;
				}

				@Override
				public  T callFunByType(CmsColumnPub pub, String queryType) {
					T re = null;
					
					// pro
					if(COLUMN_PUB.pro.name().equals(pub.getType())) {
						if( Constants.HOME_BANNER_LIST.equals(queryType) ) {
							re = (T) this.reProductVo(pub, queryType);
						} else if( Constants.VIP_HOT_BUY.equals(queryType) || Constants.NEW_PRO_RECOMMEND.equals(queryType) ) {
							re = (T) this.reBannerVo(pub, queryType);
						}
					} else if(COLUMN_PUB.news.name().equals(pub.getType())) {
						// news
						
					} else if(COLUMN_PUB.ad.name().equals(pub.getType())) {
						// ad
						
					}
					
					return re;
				}
				
			};
			
			// 转化为显示
			for(CmsColumnPub pub : pubs) {
				
				Object obj = mTypeSwitch.callFunByType(pub, queryType);
				pubOjbs.add(obj);
			}
			
			res.setData((T)pubOjbs);
		}
		res.setServiceCode(ServiceCode.SUCCESS);
		return res;
	}
	
	public static interface TypeSwitch {
		ProductVo reProductVo(CmsColumnPub pub, String queryType);
		BannerVo reBannerVo(CmsColumnPub pub, String queryType);
		<T> T callFunByType(CmsColumnPub pub, String queryType);
		
	}
}
